import 'package:flutter/material.dart';

class CommonDialog extends StatelessWidget {
  const CommonDialog({
    Key? key,
    this.title,
    this.titleColor,
    this.content,
    this.titleWidget,
    this.contentWidget,
    this.confirmTitle,
    this.cancelTitle,
    this.onConfirmPressed,
    this.onCancelPressed,
    this.cancelButtonColor,
    this.titleFontSize,
    this.hiddenTitle = false,
    this.onlyConfirm = false,
  }) : super(key: key);

  final bool hiddenTitle;

  final double? titleFontSize;

  final String? title;

  final String? content;

  final Widget? titleWidget;

  final Widget? contentWidget;

  final String? confirmTitle;

  final String? cancelTitle;

  final VoidCallback? onConfirmPressed;

  final VoidCallback? onCancelPressed;

  final bool onlyConfirm;

  final Color? cancelButtonColor;

  final Color? titleColor;

  @override
  Widget build(BuildContext context) {
    final Widget dialogTitle = Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Expanded(
          child: titleWidget ??
              Text(
                title ?? '',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: titleFontSize ?? 16,
                  fontWeight: FontWeight.bold,
                  color: titleColor ?? Colors.black,
                ),
              ),
        ),
      ],
    );

    final Widget dialogContent = contentWidget ??
        Text(
          this.content ?? '',
          textAlign: TextAlign.center,
          softWrap: true,
          style: const TextStyle(fontSize: 14),
        );

    final Widget bottomButton = onlyConfirm
        ? TextButton(
            onPressed: onConfirmPressed,
            style: ButtonStyle(
              foregroundColor: MaterialStateProperty.resolveWith(
                (states) {
                  return Colors.white;
                },
              ),
              backgroundColor: MaterialStateProperty.resolveWith((states) {
                return const Color(0xFFFF8400);
              }),
            ),
            child: Text(
              confirmTitle ?? 'Yes',
            ),
          )
        : Column(
            children: [
              TextButton(
                onPressed: onConfirmPressed,
                style: ButtonStyle(
                  foregroundColor: MaterialStateProperty.resolveWith(
                    (states) {
                      return Colors.white;
                    },
                  ),
                  backgroundColor: MaterialStateProperty.resolveWith((states) {
                    return const Color(0xFFFF8400);
                  }),
                ),
                child: Text(
                  confirmTitle ?? 'Yes',
                ),
              ),
              const SizedBox(
                height: 16,
              ),
              TextButton(
                onPressed: onCancelPressed ?? () => Navigator.pop(context),
                style: ButtonStyle(
                  foregroundColor: MaterialStateProperty.resolveWith(
                    (states) {
                      return Colors.white;
                    },
                  ),
                  backgroundColor: MaterialStateProperty.resolveWith((states) {
                    return Colors.transparent;
                  }),
                ),
                child: Text(
                  confirmTitle ?? 'Cancel',
                ),
              )
            ],
          );

    final Widget content = Material(
      borderRadius: BorderRadius.circular(16.0),
      child: Container(
        padding:
            const EdgeInsets.only(left: 30, right: 30, bottom: 30, top: 10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            if (!hiddenTitle) ...[
              const SizedBox(
                height: 16,
              ),
              dialogTitle,
            ],
            const SizedBox(
              height: 16,
            ),
            dialogContent,
            const SizedBox(
              height: 16,
            ),
            bottomButton,
          ],
        ),
      ),
    );

    final Widget body = MediaQuery.removeViewInsets(
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      context: context,
      child: Center(
        child: SizedBox(
          width: 270,
          child: content,
        ),
      ),
    );
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets,
      duration: const Duration(milliseconds: 120),
      curve: Curves.easeInCubic, // easeOutQuad
      child: body,
    );
  }
}
